package com.nondo.dean.dealer.service;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.nondo.dean.dealer.domain.ActivityCount;
import com.nondo.dean.dealer.domain.ActivityCountExample;
import com.nondo.dean.dealer.mapper.ActivityCountMapper;

@Component
public class ActivityCountService {

    @Autowired
    ActivityCountMapper activityCountMapper;

    /**
     * 添加
     *
     * @param activityCount
     * @return
     */
    @Transactional
    public int add(ActivityCount activityCount) {
        return this.activityCountMapper.insertSelective(activityCount);
    }

    /**
     * 更新
     *
     * @param activityCount
     * @return
     */
    @Transactional
    public int update(ActivityCount activityCount) {
        return this.activityCountMapper.updateByPrimaryKeySelective(activityCount);
    }

    /**
     * 删除
     *
     * @param activityCount
     * @return
     */
    public int delete(ActivityCount activityCount) {
        activityCount.setDelFlag(ActivityCount.DEL_FLAG_DELETE);
        return this.activityCountMapper.updateByPrimaryKeySelective(activityCount);
    }

    /**
     * 根据主键查询
     *
     * @param id
     * @return
     */
    public ActivityCount select(Integer id) {
        return this.activityCountMapper.selectByPrimaryKey(id);
    }

    /**
     * 获取列表
     *
     * @param offset
     * @param limit
     * @param activityCount
     * @return
     */
    public List<ActivityCount> getList(int offset, int limit, ActivityCount activityCount) {
        ActivityCountExample example = new ActivityCountExample();
        example.setOrderByClause("create_date DESC");
        RowBounds rowBounds = new RowBounds(offset, limit);
        ActivityCountExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, activityCount);
        return activityCountMapper.selectByExampleWithRowbounds(example, rowBounds);
    }

    /**
     * 数据统计
     *
     * @param activityCount
     * @return
     */
    public Long count(ActivityCount activityCount) {
        ActivityCountExample example = new ActivityCountExample();
        ActivityCountExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, activityCount);
        return activityCountMapper.countByExample(example);
    }

    /**
     * 设置查询条件
     *
     * @param criteria
     * @param activityCount
     */
    void setCriteria(ActivityCountExample.Criteria criteria, ActivityCount activityCount) {
        if (activityCount != null) {
            if (activityCount.getId() != null) {
                criteria.andIdEqualTo(activityCount.getId());
            }
        }
        criteria.andDelFlagEqualTo(ActivityCount.DEL_FLAG_NORMAL);
    }

    /**
     * 按照活动ID查询活动
     * @param activityId 活动ID
     * @return {@link ActivityCount}
     */
    public ActivityCount selectByActivityId(Integer activityId) {
        ActivityCountExample example = new ActivityCountExample();
        ActivityCountExample.Criteria criteria = example.createCriteria();
        criteria.andActivityIdEqualTo(activityId);
        List<ActivityCount> activityCountList = activityCountMapper.selectByExample(example);
        return CollectionUtils.isNotEmpty(activityCountList) ? activityCountList.get(0) : null;
    }

}
